require('type')
local ascii = require('ascii')
local KEY = require('key')

-- 引擎接口
local Galaxy = {}

-- 弹框
function Galaxy:msgbox(title, msg)
    if isNil(msg) then
        msg = title
        title = 'ggesoeasy'
    end
    return cc_msgbox(ascii(title), ascii(msg))
end

-- 窗口大小
function Galaxy:setWindowSize(w, h)
    return gge_setWindowSize(isNumber(w) and w or 640, isNumber(h) and h or 480)
end

-- 隐藏鼠标
function Galaxy:hideMouse(bool)
    return gge_hideMouse(not not bool)
end

-- 是否写入日志
function Galaxy:debugLog(bool)
    return gge_debugLog(not not bool)
end

-- 纹理过滤
function Galaxy:forceTexturefilter(bool)
    return gge_forceTexturefilter(not not bool)
end

-- 窗口标题
function Galaxy:setWindowTitle(title)
    return gge_setWindowTitle(ascii(title))
end

-- 窗口句柄
function Galaxy:getHwnd()
    return gge_getHwnd()
end

-- 写入日志
function Galaxy:log(text)
    return gge_log(ascii(text))
end

-- 运行/打开url
function Galaxy:launch(url)
    return System_Launch(ascii(url))
end

-- 设置配置文件ini
function Galaxy:setIni(inifile)
    return Ini_SetFile(ascii(inifile))
end

-- 各种ini操作 下次再写


-- 帧间隔(秒)
function Galaxy:getDeltaTime()
    return Timer_GetDelta()
end

-- 运行时间(毫秒)
function Galaxy:getRunningTime()
    return Timer_GetTime()
end

function Galaxy:random(min, max)
    if (not isNumber(min)) and (not isNumber(max)) then
        min = 0
        max = 99
    end
    if not isNumber(max) then
        if isNumber(min) then
            max = min
            min = 0
        end
    end
    if not isNumber(min) then
        min = 0
    end
    return Random_Int(min, max, 0)
end


-- 开始渲染
function Galaxy:renderBegin(texture, color)
    local bool = gge_beginScene(isPointer(texture) and texture or nullptr)
    -- if bool then Graph_Clear(isNumber(color) and color or 0) end
    Graph_Clear(isNumber(color) and color or 0)
    return bool
end

-- 结束渲染
function Galaxy:renderEnd()
    return Graph_EndScene(true)
end

-- 鼠标
function Galaxy:getMouse()
    return Input_GetMousePosX(), Input_GetMousePosY()
end
function Galaxy:getMouseX()
    return Input_GetMousePosX()
end
function Galaxy:getMouseY()
    return Input_GetMousePosY()
end

-- 左键按住
function Galaxy:isMpress(key)
    return Input_IsMousePress(isNumber(key) and key or KEY.LBUTTON)
end

-- 左键弹起
function Galaxy:isLup()
    return Input_IsMouseUp(KEY.LBUTTON)
end

-- 左键按下
function Galaxy:isLdown()
    return Input_IsMouseDown(KEY.LBUTTON)
end

-- 右键弹起
function Galaxy:isRup()
    return Input_IsMouseUp(KEY.RBUTTON)
end

-- 右键按下
function Galaxy:isRdown()
    return Input_IsMouseDown(KEY.RBUTTON)
end

-- 滚轮
function Galaxy:getMouseWheel()
    return Input_GetMouseWheel()
end

-- 按键按住
function Galaxy:isKpress(key)
    if not isNumber(key) then
        return false
    end
    return Input_IsKeyPress(key)
end

-- 按键弹起
function Galaxy:isKup(key)
    if not isNumber(key) then
        return false
    end
    return Input_IsKeyUp(key)
end

-- 按键按下
function Galaxy:isKdown(key)
    if not isNumber(key) then
        return false
    end
    return Input_IsKeyDown(key)
end

-- 获取输入字符串
function Galaxy:getChar()
    return Variant_Asc2Utf8(Input_GetChar()) or string.empty
end

-- 颜色
function Galaxy:ARGB(a, r, g, b)
    return Color_ARGB(
        isNumber(a) and a or 0, 
        isNumber(r) and r or 0, 
        isNumber(g) and g or 0, 
        isNumber(b) and b or 0)
end

function Galaxy:RGB(r, g, b)
    return Color_ARGB(0xFF,
        isNumber(r) and r or 0, 
        isNumber(g) and g or 0, 
        isNumber(b) and b or 0)
end

-- 音乐
local curr_music_name = string.empty
local curr_music = nullptr
function Galaxy:playMusic(filename, volume)
    if filename == curr_music_name then
        if  curr_music ~= nullptr then
            music_volume(curr_music, isNumber(volume) and volume or 100)
        end
        return
    end
    if  curr_music ~= nullptr then
        music_release(curr_music)
    end
    curr_music = music_load(ascii(filename))
    if curr_music == nullptr then
        curr_music_name = string.empty
        return
    end
    music_volume(curr_music, isNumber(volume) and volume or 100)
    music_loop(curr_music, true)
    music_play(curr_music)
    curr_music_name = filename
end


-- 音效
local curr_count = 0
local sound_table = {}
function Galaxy:playSound(filename, volume)
    local sound = sound_load(ascii(filename))
    if sound == nullptr then return end
    sound_volume(sound, isNumber(volume) and volume or 100)
    sound_loop(sound, false)
    sound_play(sound)
    curr_count = curr_count + 1
    local index = curr_count % 32
    local ptr = sound_table[index]
    if ptr then 
        sound_release(ptr)
    end
    sound_table[index] = sound
end



-- 汉化
Galaxy.弹框 = Galaxy.msgbox

Galaxy.设窗口宽高 = Galaxy.setWindowSize
Galaxy.隐藏鼠标 = Galaxy.hideMouse
Galaxy.开启日志 = Galaxy.debugLog
Galaxy.纹理过滤 = Galaxy.forceTexturefilter
Galaxy.设窗口标题 = Galaxy.setWindowTitle
Galaxy.取句柄 = Galaxy.getHwnd
Galaxy.写入日志 = Galaxy.log
Galaxy.运行 = Galaxy.launch
-- ini系列
Galaxy.取毫秒 = Galaxy.getRunningTime
Galaxy.随机数 = Galaxy.random
Galaxy.渲染开始 = Galaxy.renderBegin
Galaxy.渲染清屏 = Graph_Clear
Galaxy.渲染结束 = Galaxy.renderEnd
-- mouse xy
-- Galaxy.鼠标按住 = Galaxy.isMpress
function Galaxy:左键按住() return Input_IsMousePress(KEY.LBUTTON) end
Galaxy.左键弹起 = Galaxy.isLup
Galaxy.左键按下 = Galaxy.isLdown
function Galaxy:右键按住() return Input_IsMousePress(KEY.RBUTTON) end
Galaxy.右键弹起 = Galaxy.isRup
Galaxy.右键按下 = Galaxy.isRdown
Galaxy.取滚轮 = Galaxy.getMouseWheel
Galaxy.键盘按住 = Galaxy.isKpress
Galaxy.键盘弹起 = Galaxy.isKup
Galaxy.键盘按下 = Galaxy.isKdown
Galaxy.取字符串 = Galaxy.getChar

Galaxy.颜色ARGB = Galaxy.ARGB
Galaxy.颜色RGB = Galaxy.RGB

Galaxy.播放音乐 = Galaxy.playMusic
Galaxy.播放音效 = Galaxy.playSound



return Galaxy